package role

import (
	"context"

	"mall/service/backend/model"
	"mall/service/backend/rpc/internal/svc"
	"mall/service/backend/rpc/internal/utils/errorhandler"
	"mall/service/backend/rpc/types/backend"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetRoleMenuAuthorityLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetRoleMenuAuthorityLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRoleMenuAuthorityLogic {
	return &GetRoleMenuAuthorityLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetRoleMenuAuthorityLogic) GetRoleMenuAuthority(in *backend.IDReq) (*backend.RoleMenuAuthorityResp, error) {
	var mids []uint64
	if in.Id > 0 {
		relFilterMap := make(map[string]map[string][]any)
		relFilterMap[model.SysRoleMenuRelationshipColumns.RoleID] = map[string][]any{
			"equalTo": []any{in.Id},
		}
		rel, _, err := l.svcCtx.SysRole.GetRelMenuList(l.ctx, model.M{}, -1, relFilterMap)
		if err != nil {
			return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
		}

		if len(rel) > 0 {
			for _, v := range rel {
				mids = append(mids, v.MenuID)
			}
		}
	}

	return &backend.RoleMenuAuthorityResp{
		MenuIds: mids,
	}, nil
}
